﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using Jabr.Logic.Roster;

namespace Jabr.GUI.Forms
{
    public partial class AccountAll : Form
    {
        public AccountAll()
        {
            InitializeComponent();
        }

        private void AccountAll_Load(object sender, EventArgs e)
        {
            AccountBindingSource.DataSource = RosterSingleton.Instance.AccountCollection;
        }

        private void NewBtn_Click(object sender, EventArgs e)
        {
            AccountAdd aa = new AccountAdd();
            aa.ShowDialog();
            if(aa.AccountCreated == null)
                return;
            AccountGenericEdit age = (AccountGenericEdit)Assembly.GetAssembly(typeof(AccountGenericEdit)).GetType(aa.AccountCreated.Form, true, true).GetConstructor(new Type[] { typeof(AbstractAccount) }).Invoke(new object[] { aa.AccountCreated });
            if (age.ShowDialog() == DialogResult.OK)
            {
                RosterSingleton.Instance.Add(aa.AccountCreated);
                AccountBindingSource.ResetBindings(false);
            }
        }

        private void AccountView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            AbstractAccount ja = (AbstractAccount)((ICloneable)AccountBindingSource.Current).Clone();
            AccountGenericEdit age = (AccountGenericEdit)Assembly.GetAssembly(typeof(AccountGenericEdit)).GetType(ja.Form, true, true).GetConstructor(new Type[] { typeof(AbstractAccount) }).Invoke(new object[] { ja });
            if (age.ShowDialog() == DialogResult.OK && ja.CompareTo((AbstractAccount)AccountBindingSource.Current) != 0)
            {
                ((AbstractAccount)AccountBindingSource.Current).Display.Account = ja;
                ja.Display = ((AbstractAccount)AccountBindingSource.Current).Display;
                ja.Presence = GenericPresence.OFFLINE;
                AccountBindingSource[AccountBindingSource.Position] = ja;
            }
        }

        private void AccountView_MouseDown(object sender, MouseEventArgs e)
        {
            var hit = AccountView.HitTest(e.X, e.Y);
            switch (hit.Type)
            {
                case DataGridViewHitTestType.Cell:
                    if (e.Button == MouseButtons.Right)
                    {
                        this.AccountView[hit.ColumnIndex, hit.RowIndex].Selected = true;
                        this.editToolStripMenuItem.Enabled = this.deleteToolStripMenuItem.Enabled = true;
                    }
                    break;
                default:
                    this.editToolStripMenuItem.Enabled = this.deleteToolStripMenuItem.Enabled = false;
                    break;
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.NewBtn_Click(sender, e);
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.AccountView_CellDoubleClick(sender, null);
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RosterSingleton.Instance.Delete((AbstractAccount)AccountBindingSource.Current);
            AccountBindingSource.ResetBindings(false);
        }
    }
}
